package com.eben.GUI;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import com.eben.model.Board;
import com.eben.model.Board.VibrationException;
import com.eben.model.GameObject;
import com.eben.model.GameOverException;
import com.eben.model.Life;
import com.eben.model.Position;
import com.eben.model.Score;


public class GameView extends View {
	
	private Life life;
	private Score score;
	private ArrayList<GameObject> gameObjects = new ArrayList<GameObject>();
	private Board gameBoard;;

    public GameView(Context context) {
        super(context);
            
        //gets the display
        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        
        //creates new board with the size of the screen
        gameBoard = new Board(new Position(display.getWidth(),display.getHeight()));						
        gameObjects = gameBoard.getBoardObjects();
        life = gameBoard.getLife();
        score = gameBoard.getScore();
    }

    @Override
    public void onDraw(Canvas canvas) {
    	if(life.isInvincible() == true){
    		life.drawInvincibleTime(canvas, (float)gameBoard.getInvincibleActiveTimer().getTime());
    	} else {
    		life.draw(canvas);
    	}
    	score.draw(canvas);
    	for(int i=0;i<gameObjects.size();i++){
    		gameObjects.get(i).draw(canvas);
    	}
    }
    
    public void updateCursor(Position position){
    	gameBoard.updateCursor(position);
    }
    
    public void updatePositions() throws GameOverException, VibrationException{
		gameBoard.updatePositions();
    }
    
    public Position getCursorPosition(){
    	return gameBoard.getPointer().getPosition();
    }

    /**
     * Starts the game.
     */
	public void startGame() {
		gameBoard.startGame();
	}
}
